package net.shouyong.topbilliards.bean;

import android.os.Parcel;

import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.SaveCallback;

import net.shouyong.topbilliards.Utils;

import java.util.Arrays;
import java.util.Date;

/**
 * 记录
 * Created by shouyong on 2016/10/24.
 */
@AVClassName("BilliardRecord")
public class BilliardRecord extends AVObject {

    //此处为我们的默认实现，当然你也可以自行实现
    public static final Creator CREATOR = AVObjectCreator.instance;

    public static final String INDEX = "index";
    public static final String START_TIME = "startTime";

    public static final String END_TIME = "endTime";
    public static final String COST = "cost"; //计算金额
    public static final String unitPrice = "unitPrice"; //单价    元/小时

    public static final String SETTLEMENT = "settlement"; //实收金额

    public static final String COMMENT = "comment"; //备注

    public BilliardRecord(){
        super();
    }

    public BilliardRecord(Parcel in){
        super(in);
    }

    public int getIndex(){
        return getInt(INDEX);
    }


    public void setIndex(int index){
        put(INDEX, index);
    }


    public boolean isStart(){
        return getStartTime() != null;
    }

    public String getStartTime(){
        return getString(START_TIME);
    }

    public void setStartTime(Date date){
        put(START_TIME, Utils.dateToString(date));
    }

    public Date getEndTime(){
        return getDate(END_TIME);
    }

    public void setEndTime(Date endTime){
        put(END_TIME, endTime);
    }

    public int getCost(){

        Date startTime = Utils.stringToDate(getStartTime());
        if (startTime == null) {
            return 0;
        }

        Date endTime = getEndTime();
        if (endTime == null){
            endTime = new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
        }

        long between = (endTime.getTime() - startTime.getTime())/1000;//除以1000是为了转换成秒
        if (between <= 0){
            return 0;
        }
        long minute1 = between /60;

        int unitPrice = getUnitPrice();
        if (unitPrice <= 0){
            return 0;
        }

        int  cost = (int) minute1 * unitPrice/60;
        put(COST, cost);
        return cost;
    }

    public int getUnitPrice(){
        return getInt(unitPrice);
    }

    public void setUnitPrice(int uPrice){
        put(unitPrice, uPrice);
    }

    public int getSettlement(){
        return getInt(SETTLEMENT);
    }

    public void  setSettlement(int settlement){
        put(SETTLEMENT, settlement);
    }

    public String getComment(){
        return getString(COMMENT);
    }

    public void setComment(String comment){
        put(COMMENT, comment);
    }
}
